package com.lecyon.farm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.lecyon.farm.base.RestResponse;
import com.lecyon.farm.common.CommonService;
import com.lecyon.farm.common.constant.CommonConstant;
import com.lecyon.farm.common.enums.SysRoleEnum;
import com.lecyon.farm.entity.FmCar;
import com.lecyon.farm.entity.JsSysEmployee;
import com.lecyon.farm.entity.JsSysOffice;
import com.lecyon.farm.entity.JsSysUser;
import com.lecyon.farm.mapper.FmCarMapper;
import com.lecyon.farm.service.IFmCarService;
import com.lecyon.farm.service.IJsSysEmployeeService;
import com.lecyon.farm.service.IJsSysOfficeService;
import com.lecyon.farm.util.RedisUtils;
import com.lecyon.farm.util.ResultUtil;
import com.lecyon.farm.util.StringHelper;
import com.lecyon.farm.util.ValidateUtils;
import com.lecyon.farm.vo.data.DeviceAccessStatisticsVo;
import com.lecyon.farm.vo.query.QueryFmCarVo;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 农机基本信息表 服务实现类
 * </p>
 *
 * @author YaoZheng
 * @since 2020-04-02
 */
@Service
@Validated
public class FmCarServiceImpl extends ServiceImpl<FmCarMapper, FmCar> implements IFmCarService {

    private CommonService commonService;

    private IJsSysOfficeService jsSysOfficeService;

    private IJsSysEmployeeService jsSysEmployeeService;

    private FmCarMapper fmCarMapper;

    private RedisUtils redisUtils;

    /**
     * 根据登录token获取设备接入分布数
     *
     * @param token
     * @return
     */
    @Override
    public RestResponse<List<DeviceAccessStatisticsVo>> getDeviceAccessStatisticsResult(@NotBlank String token) {
        RestResponse<List<DeviceAccessStatisticsVo>> result = new RestResponse<>();
        List<DeviceAccessStatisticsVo> resultList = Lists.newLinkedList();
        JsSysUser sysUser = commonService.findUserByToken(token);
        if (ValidateUtils.isNullOrEmpty(sysUser)) {
            return ResultUtil.error(result, CommonConstant.ASSOCIATED_DATA_MISS, "找不到登录用户相关数据");
        }
        String userCode = sysUser.getUserCode();
        JsSysEmployee jsSysEmployee = jsSysEmployeeService.getById(userCode);
        String officeCode = null;
        if (ValidateUtils.isNotNullOrEmpty(jsSysEmployee) && StringHelper.isNotEmpty(jsSysEmployee.getOfficeCode())) {
            officeCode = jsSysEmployee.getOfficeCode();
        }
        QueryWrapper<FmCar> fmCarQueryWrapper = new QueryWrapper<>();
        QueryFmCarVo queryCondition = new QueryFmCarVo();
        List<FmCar> fmCarList;

        if (SysRoleEnum.MASTER.getLevel().equals(sysUser.getSign())) {
            // 机构管理员 可以看到所辖区域内所有相关信息
            // GR代表个人用户
            String specialOfficeCode = "GR";
            if (StringHelper.isNotEmpty(officeCode) && !specialOfficeCode.equals(officeCode)) {
                JsSysOffice jsSysOffice = jsSysOfficeService.getById(officeCode);
                if (ValidateUtils.isNotNullOrEmpty(jsSysOffice) && StringHelper.isNotEmpty(jsSysOffice.getParentCodes())) {
                    String parentCodes = jsSysOffice.getParentCodes();
                    if (parentCodes.contains(CommonConstant.HARDWARE_RESPONSE_SPLIT_STR)) {
                        String[] parentCode = parentCodes.split(CommonConstant.HARDWARE_RESPONSE_SPLIT_STR);
                        int codeLength = parentCode.length;
                        switch (codeLength) {
                            case 1:
                                // 省级机构
                                fmCarQueryWrapper.eq("province", officeCode);
                                queryCondition.setProvince(officeCode);
                                resultList = fmCarMapper.selectStatisticGroupByCity(queryCondition);
                                break;
                            case 2:
                                // 市级机构
                                fmCarQueryWrapper.eq("province", parentCode[1]).eq("city", officeCode);
                                queryCondition.setCity(officeCode);
                                resultList = fmCarMapper.selectStatisticGroupByCounty(queryCondition);
                                break;
                            default:
                                // 区县机构
                                fmCarQueryWrapper.eq("province", parentCode[1]).eq("city", parentCode[2])
                                        .eq("county", officeCode);
                                queryCondition.setCounty(officeCode);
                                resultList = fmCarMapper.selectStatisticGroupByTowns(queryCondition);
                                break;
                        }
                    }
                }
            }
        }
        return ResultUtil.success(result,resultList);
    }

    /**
     * ----------------------------- 分割线 以下是setter方法，用于替换field上的@Autowired -----------------------------
     **/

    @Autowired
    @Lazy
    public void setCommonService(CommonService commonService) {
        this.commonService = commonService;
    }

    @Autowired
    @Lazy
    public void setJsSysOfficeService(IJsSysOfficeService jsSysOfficeService) {
        this.jsSysOfficeService = jsSysOfficeService;
    }

    @Autowired
    @Lazy
    public void setJsSysEmployeeService(IJsSysEmployeeService jsSysEmployeeService) {
        this.jsSysEmployeeService = jsSysEmployeeService;
    }

    @Autowired
    @Lazy
    public void setRedisUtils(RedisUtils redisUtils) {
        this.redisUtils = redisUtils;
    }

    @Autowired
    @Lazy
    public void setFmCarMapper(FmCarMapper fmCarMapper) {
        this.fmCarMapper = fmCarMapper;
    }
}
